package com.tanhua.dubbo.async;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 异步线程处理类
 */
@Component
@Slf4j
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async //异步处理线程注解
    public void addTimeLine(Movement movement, Long userId, long nowTime, List<Friend> friendList) {
        if(!CollectionUtils.isEmpty(friendList)){
            log.debug("***********************异步处理时间线表数据开始了*******************");
            //获取动态id
            ObjectId movementId = movement.getId();
            for (Friend friend : friendList) {
                MovementTimeLine movementTimeLine = new MovementTimeLine();
                movementTimeLine.setMovementId(movementId);//动态主键id
                movementTimeLine.setUserId(userId);//动态发布用户id
                movementTimeLine.setFriendId(friend.getFriendId());//好友id
                movementTimeLine.setCreated(nowTime);
                mongoTemplate.insert(movementTimeLine);
            }
            log.debug("***********************异步处理时间线表数据结束了*******************");
        }
    }
}
